package com.data.structure.queue;

import java.util.Scanner;

/**
 * 环形数模拟队列
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/23
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        System.out.println("测试数组模拟环形队列的案例~~~");
        CircleArray queue = new CircleArray(4);
        char key ;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);
            int res;
            switch(key) {
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'g':
                    try {
                        res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception var8) {
                        System.out.println(var8.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception var7) {
                        System.out.println(var7.getMessage());
                    }
                    break;
                case 's':
                    queue.showQueue();
            }
        }

        System.out.println("程序退出~~");
    }
}

class CircleArray {
    /**
     * 数组最大容量
     */
    private int maxSize;
    /**
     * 队列头，指向队列第一个元素
     */
    private int head;
    /**
     * 队列尾，指向队列最后一个元素的后一个完位置
     */
    private int tail;
    /**
     * 存放数据的数组
     */
    private int[] arr;

    public CircleArray(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        this.arr = new int[this.maxSize];
    }

    public boolean isFull() {
        return (this.tail + 1) % this.maxSize == this.head;
    }

    public boolean isEmpty() {
        return this.tail == this.head;
    }

    public void addQueue(int n) {
        if (this.isFull()) {
            System.out.println("队列满，不能加入数据~");
        } else {
            this.arr[this.tail] = n;
            this.tail = (this.tail + 1) % this.maxSize;
        }
    }

    public int getQueue() {
        if (this.isEmpty()) {
            throw new RuntimeException("队列空，不能取数据");
        } else {
            int value = this.arr[this.head];
            this.head = (this.head + 1) % this.maxSize;
            return value;
        }
    }

    public void showQueue() {
        if (this.isEmpty()) {
            System.out.println("队列空的，没有数据~~");
        } else {
            for(int i = this.head; i < this.head + this.size(); ++i) {
                System.out.printf("arr[%d]=%d\n", i % this.maxSize, this.arr[i % this.maxSize]);
            }

        }
    }

    public int size() {
        return (this.tail + this.maxSize - this.head) % this.maxSize;
    }

    public int headQueue() {
        if (this.isEmpty()) {
            throw new RuntimeException("队列空的，没有数据~~");
        } else {
            return this.arr[this.head];
        }
    }
}
